// // alert("click in main")
//
// var a = [1,2,3,4,5,7];
// var b = [2,4,6,7];
// var c = [];
//
// for(var i = 0, j = 0; i < b.length; i++){
//   console.log(i);
//   if(b[i] > a[j] && b[i] < a[j + 1]){
//     continue;
//   }
//   if(b[i] > a[j]){
//     j++;
//     i--;
//   } else if(b[i] === a[j]){
//     c.push(b[i]);
//     j++;
//   }
//   console.log(c);
// }

var arr1 = [1, 2, 3, 3, 4, 5];
var arr2 = [1, 3, 4, 5, 6, 6];
var arrResult = [];

let j = 0;  // arr1 index
let k = 0;  // arr2 index
let l = 0;  // arrResult index
for(let i = 0; i < arr1.length + arr2.length; i++){

  if (arr2[k] !== arr1[j]) {
    arrResult[l++] = arr1[j] < arr2[k]? arr1[j++]: arr2[k++];
    // console.log('j:' + j + ' k:' + k + ' l:' + l);
  } else {
    console.log('j:' + j + ' k:' + k + ' l:' + l);
    let arr1Count = countElement(arr1[j], arr1);
    let arr2Count = countElement(arr2[k], arr2);
    if(arr1Count >= arr2Count) {
      for(let m = 0; m < arr1Count; m++){
        arrResult[l++] = arr1[j++];
      }
      k += arr2Count;
    }  else {
      for(let m = 0; m < arr2Count; m++) {
        arrResult[l++] = arr2[k++];
      }
      j += arr1Count;
    }

  }
}

function countElement(element, arr){
  var count = 0;
  for(let i = 0; i < arr.length; i++) {
    if(element === arr[i]) {
      count++;
    }
  }
  console.log('element:' + element + ' arr:' + arr + ' count:' + count);
  return count;
}
console.log(arrResult);
